from flask import Blueprint,render_template,request,jsonify,redirect,url_for
#from utils import db
from ..models.User import User
from exts import db


reg=Blueprint('register',__name__)

@reg.route('/r')
def register_view():
    return render_template('register.html')


@reg.route('/register',methods=['post'])
def register_handle():
    print("1111")
    data = request.json  # 获取JSON格式的请求数据
    username = data.get('username')
    password = data.get('password')
    user = User.query.filter_by(userName=username).first()
    if user is None:
        new_user=User(userName=username, password=password)
        print(new_user)
        db.session.add(new_user)
        db.session.commit()
        return jsonify({'status': 'success', 'message': '注册成功'})

    return jsonify({'status': 'fail', 'message': '用户名已存在'})